package cc.orange.utils.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cc.orange.R;
import cc.orange.adapter.HomePopupAdapter;
import cc.orange.utils.TwoWayRattingBar;

/**
 */

public class PopupHomeSpeedView {

    private static View inflate;
    private static HomePopupAdapter adapter;
    private static PopupWindow popupWindow;
    private static String num = "1";
    public static void showPopupwindow(final Context context,
                                       View view,PopuStatus pp) {//回调

        if (popupWindow == null && inflate == null) {
            inflate = LayoutInflater.from(context).inflate(R.layout.item_popu_home_speed, null);
            popupWindow = new PopupWindow(PopupHomeSpeedView.inflate, RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        }
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        //设置位
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        backgroundAlpha(0.3f, (Activity) context);

//        popupWindow.showAsDropDown(view);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f,(Activity) context);

            }
        });

        TextView tvPopPeople1 = inflate.findViewById(R.id.tv_pop_people_1);
        TextView tvPopPeople3 = inflate.findViewById(R.id.tv_pop_people_3);
        TextView tvPopPeople5 = inflate.findViewById(R.id.tv_pop_people_5);
        TextView tvPopPeople10 = inflate.findViewById(R.id.tv_pop_people_10);

        TextView tvAgeL = inflate.findViewById(R.id.tv_age_l);
        TextView tvAgeR = inflate.findViewById(R.id.tv_age_r);

        tvPopPeople1.setOnClickListener(view15 -> {
            num = "1";
            tvPopPeople1.setBackgroundResource( R.drawable.bg_pop_speed);
            tvPopPeople1.setTextColor(context.getResources().getColor(R.color.color_ef43ff));
            tvPopPeople3.setBackground(null);
            tvPopPeople3.setTextColor(context.getResources().getColor(R.color.color_333));
            tvPopPeople5.setBackground(null);
            tvPopPeople5.setTextColor(context.getResources().getColor(R.color.color_333));
            tvPopPeople10.setBackground(null);
            tvPopPeople10.setTextColor(context.getResources().getColor(R.color.color_333));
        });
        tvPopPeople3.setOnClickListener(view14 -> {
            num = "3";
            tvPopPeople3.setBackgroundResource( R.drawable.bg_pop_speed);
            tvPopPeople3.setTextColor(context.getResources().getColor(R.color.color_ef43ff));
            tvPopPeople1.setBackground(null);
            tvPopPeople1.setTextColor(context.getResources().getColor(R.color.color_333));
            tvPopPeople5.setBackground(null);
            tvPopPeople5.setTextColor(context.getResources().getColor(R.color.color_333));
            tvPopPeople10.setBackground(null);
            tvPopPeople10.setTextColor(context.getResources().getColor(R.color.color_333));
        });
        tvPopPeople5.setOnClickListener(view13 -> {
            num = "5";
            tvPopPeople5.setBackgroundResource( R.drawable.bg_pop_speed);
            tvPopPeople5.setTextColor(context.getResources().getColor(R.color.color_ef43ff));
            tvPopPeople3.setBackground(null);
            tvPopPeople3.setTextColor(context.getResources().getColor(R.color.color_333));
            tvPopPeople1.setBackground(null);
            tvPopPeople1.setTextColor(context.getResources().getColor(R.color.color_333));
            tvPopPeople10.setBackground(null);
            tvPopPeople10.setTextColor(context.getResources().getColor(R.color.color_333));
        });
        tvPopPeople10.setOnClickListener(view12 -> {
            num = "10";
            tvPopPeople10.setBackgroundResource( R.drawable.bg_pop_speed);
            tvPopPeople10.setTextColor(context.getResources().getColor(R.color.color_ef43ff));
            tvPopPeople3.setBackground(null);
            tvPopPeople3.setTextColor(context.getResources().getColor(R.color.color_333));
            tvPopPeople5.setBackground(null);
            tvPopPeople5.setTextColor(context.getResources().getColor(R.color.color_333));
            tvPopPeople1.setBackground(null);
            tvPopPeople1.setTextColor(context.getResources().getColor(R.color.color_333));
        });

        RelativeLayout relativeLayout = inflate.findViewById(R.id.home_popu_text2);

        TwoWayRattingBar twoWayRattingBar = inflate.findViewById(R.id.twr_pop_ratting);

        ImageView homePopuImgs2 = inflate.findViewById(R.id.home_popu_imgs2);

        relativeLayout.setOnClickListener(view1 -> {
            if (pp != null) {
                pp.popupDismissClick2(num);
            }
        });

        twoWayRattingBar.setOnProgressChangeListener(new TwoWayRattingBar.OnProgressChangeListener() {
            @Override
            public void onLeftProgressChange(float progress) {
                int age = (int) (60 * progress);
                if (age < 18) {
                    age = 18;
                }
                tvAgeL.setText(age + "岁");
            }

            @Override
            public void onRightProgressChange(float progress) {
                int age = (int) (60 * progress);
                if (age < 18) {
                    age = 18;
                }
                tvAgeR.setText(age + "岁");
            }

            @Override
            public void onActionUp() {
                Log.e("steven", "onActionUp: " );
            }
        });
        homePopuImgs2.setOnClickListener(v -> {
            popupWindow.dismiss();
            pp.popupDismissCancle("");
            twoWayRattingBar.setReSatatus();
        });

        pp.popupShow();
    }

    public static void backgroundAlpha(float bgAlpha, Activity context) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }

    public static void popDismiss() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }


    public interface PopuStatus {
        void popupShow();
        void popupDismissClick2(String se);
        void popupDismissCancle(String se);

    }
}
